// pages/self-assessment/video-assessment/video.js
const api = require("../../../config/api")
const request = require("../../../utils/request")
const moment = require('./../../../utils/moment');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		showModalStatus: false,
		patientList: '',
		patientInfo: ''
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {

	},
	// 获取患者列表
	getPatientList: async function() {
		const userId = wx.getStorageSync('userId')
		const res = await request.get(api.getPatientList + '/' + userId)
		if (res.httpStatus === 200) {
			if (res.data.length > 0) {
				let nowDeliveryDate = moment() //获取当前日期
				for (const item of res.data) {
					let ageText = ''
					let deliveryDate = moment(item.birthday); //用moment来显示获取的日期
					let age = moment.duration(moment().diff(deliveryDate)); //作对比.显示天数
					let {
						_data
					} = age
					if (_data.years) {
						ageText = _data.years + '岁' + _data.months + '月'
					} else if (_data.months) {
						ageText = _data.months + '月'
					} else {
						ageText = _data.days + '天'
					}
					item.age = ageText
					if (item.isDefault === 1) {
						this.setData({
							patientInfo: item,
						})
					}
				}
				this.setData({
					patientList: res.data
				})
			}
		}
	},
	changeBtn() {
		this.setData({
			showModalStatus: true
		})
	},
	begin() {
		if (this.data.patientList.length > 0) {
			this.canSave()
		} else {
			wx.navigateTo({
				url: './creatPatient'
			})
		}
	},
	// 切换患者
	patientChange: function(e) {
		this.setData({
			patientInfo: e.detail
		})
	},
	canSave: async function() {
		const userId = wx.getStorageSync('userId')
		const patientInfo = this.data.patientInfo
		const data = {
			userId: userId,
			patientId: patientInfo.id
		}
		const res = await request.get(api.canSave, data)
		if (res.httpStatus === 200) {
			if (res.data) {
				const data = JSON.stringify(this.data.patientInfo)
				wx.navigateTo({
					url: './highRiskInfo?patient=' + data
				})
			} else {
				wx.showModal({
					content: '您有一条待评估视频，评估完成后才能上传新的评估视频。',
					cancelText: "取消", //默认是“取消”
					confirmText: "查看记录", //默认是“确定”
					success: function(res) {
						if (res.cancel) {
							//点击取消,默认隐藏弹框
						} else {
							//点击确定
							wx.navigateTo({
								url: '../../evaluatedRecords/index'
							})
						}
					},
					fail: function(res) {}, //接口调用失败的回调函数
					complete: function(res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）

				})
			}
		}
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		this.getPatientList()
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
